package com.anjie.powerproject.util;

import com.anjie.powerproject.entity.DataDay;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用于读取、写入csv文件
 */
public class CsvUtil {
    /**
     * 使用header、data去构建CsvData
     */
    public static CsvData buildCsvData(List<String> header, List<List<String>> data) {
        CsvData csvData = new CsvData();
        csvData.setHeader(header);
        csvData.setData(data);

        return csvData;
    }

    /**
     * 将数据写入csv文件
     * @param path 想要保存的csv的文件路径
     * @param csvData 想要保存的csv的数据
     */
    public static void dump(String path, CsvData csvData) throws IOException {
//        System.out.println("dumppath:"+path);
        if (!path.endsWith(".csv")) {
            throw new RuntimeException(String.format("传入的path不是csv文件 path:[%s]", path));
        }

        // 检查目录是否存在，如果不存在则创建目录
        int idx = path.lastIndexOf("/");
        File dir = new File(path.substring(0, idx));
        if (!dir.exists()) {
            dir.mkdirs();
        }

        FileWriter out = new FileWriter(path);
        String[] header = csvData.getHeader().toArray(new String[0]);
        try (CSVPrinter printer = new CSVPrinter(out, CSVFormat.RFC4180.withHeader(header))) {
            List<List<String>> rows = csvData.getData();
            for (List<String> row : rows) {
                printer.printRecord(row.toArray());
            }
        }
    }

    /**
     * 读取csv文件
     * @param path 想要读取的csv文件的路径
     */
    private static CsvData load(String path, CSVFormat type) throws IOException, ParseException {
        // 首先校验path，看是否是csv文件
        if (!path.endsWith(".csv")) {
            throw new RuntimeException(String.format("传入的path不是csv文件 path:[%s]", path));
        }

        CsvData csvData = new CsvData();

//        path="D:\\Anjie\\Multimode-Project_anjie\\src\\main\\resources\\static\\python\\common\\holiday.csv";

        Reader in = new FileReader(path);
        CSVParser parse = type.withFirstRecordAsHeader().parse(in);
        List<String> headers = parse.getHeaderNames();
        List<CSVRecord> records = parse.getRecords();

        List<List<String>> rows = new ArrayList<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (CSVRecord record : records) {
            List<String> row = new ArrayList<>();
            for (String h : headers) {
//                if(h.equals("\uFEFFsdate")||h.equals("sdate")){
//                    Date date=new SimpleDateFormat("yyyy/M/dd").parse(record.get(h));
//                    row.add(sdf.format(date));
//                }
//                else row.add(record.get(h));
                row.add(record.get(h));
            }
            rows.add(row);
        }
        csvData.setHeader(headers);
        csvData.setData(rows);

        return csvData;
    }

    private static CsvData load(File file, CSVFormat type) throws IOException, ParseException {
        CsvData csvData = new CsvData();

        Reader in = new FileReader(file);
        CSVParser parse = type.withFirstRecordAsHeader().parse(in);
        List<String> headers = parse.getHeaderNames();
        List<CSVRecord> records = parse.getRecords();

        List<List<String>> rows = new ArrayList<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (CSVRecord record : records) {
            List<String> row = new ArrayList<>();
            for (String h : headers) {
//                if(h.equals("\uFEFFsdate")||h.equals("sdate")){
//                    Date date=new SimpleDateFormat("yyyy/M/dd").parse(record.get(h));
//                    row.add(sdf.format(date));
//                }
//                else row.add(record.get(h));
                row.add(record.get(h));
            }
            rows.add(row);
        }
        csvData.setHeader(headers);
        csvData.setData(rows);
//        System.out.println(csvData.toString());
        return csvData;
    }

    public static CsvData loadCsv(String path) throws IOException, ParseException {
        return load(path, CSVFormat.RFC4180);
    }

    public static CsvData loadCsv(File file) throws IOException, ParseException {
        return load(file, CSVFormat.RFC4180);
    }

}
